using System;
using Gtk;
using Jandaya.Framework.Core;
using Jandaya.Framework.Core.Acesso;
using Jandaya.Framework.Dialogs;
using Jandaya.Client.Windows;


namespace Jandaya.Client.Dialogs
{
	public partial class dlgLogin : Gtk.Window
	{	
		public dlgLogin () : base(Gtk.WindowType.Toplevel)
		{
			Build ();
		}
		
		private void EnableLoginButton(){
			btnLogin.Sensitive = (
			                      ((etrUsuario.Text != null) && (etrUsuario.Text.Length > 2)) &&
			                      ((etrSenha.Text != null) && (etrSenha.Text.Length > 2))
			                     );   
		}
				
	
		protected void OnDeleteEvent (object sender, DeleteEventArgs a)
		{
			Application.Quit ();
			a.RetVal = true;
		}
		
				
		protected virtual void btnCancelarClicked (object sender, System.EventArgs e)
		{
			Application.Quit();	
		}
		
		protected virtual void btnLoginClicked (object sender, System.EventArgs e)		
		{				
			try
			{
				btnLogin.Sensitive = false;
				Usuario usr = (Usuario)App.Manager.GetNew(typeof(Usuario));
				string pResult = string.Empty;
				if (usr.Antenciar(etrUsuario.Text, etrSenha.Text, out pResult)){
					App.Authentication.SetAuthenticatedUser(usr);
					MainWindow wnd = new MainWindow();
					wnd.Show();	
					ClearEntries();
					this.Hide();
				}
				else{
					MsgDialog.Show(pResult);
				}
			}
			catch(Exception err)
			{
				MsgDialog.ShowError(err.Message);
			}
			finally{
				btnLogin.Sensitive = true;
			}
		}
		
		public void ClearEntries(){
			etrUsuario.Text = string.Empty;
			etrSenha.Text = string.Empty;
		}
		
		protected virtual void etrUsuarioChanged (object sender, System.EventArgs e)
		{		
			EnableLoginButton();
		}
		
		protected virtual void etrSenhaChanged (object sender, System.EventArgs e)
		{
			EnableLoginButton();			
		}			
		
		protected virtual void cbxConexaoChanded (object sender, System.EventArgs e)
		{
			if ((sender is ComboBox) && ((sender as ComboBox).ActiveText=="Configurar...")){
				dlgConfigDataBase dlg = new dlgConfigDataBase();
				dlg.Response += doDlgConfigDbResponse;						
				dlg.Run();				
				dlg.Destroy();				
			}
		}
		
		private void doDlgConfigDbResponse(object o, ResponseArgs args){
			switch(args.ResponseId){
			case ResponseType.Ok:
				ReloadConnParams();
				cbxConexao.Active = 1;
				break;				
			case ResponseType.Cancel:				
				break;
			}			
		}
		
		private void ReloadConnParams(){
			App.Manager.Config.LoadConfiguration();
			ResetCbxConexao();
		}
		
		private void ResetCbxConexao(){			
			
			cbxConexao.Model = new ListStore(typeof(string));
			cbxConexao.AppendText("Configurar...");
			cbxConexao.AppendText(App.Manager.Config.DBInfo.ConnectionName);
		}
		
		protected override void OnShown ()
		{
			ResetCbxConexao();
			base.OnShown();			
		}		
		
		
		protected virtual void OnEntryKeyReleased (object o, Gtk.KeyReleaseEventArgs args)
		{
			if (((args.Event.Key == Gdk.Key.Return)||(args.Event.Key == Gdk.Key.KP_Enter)) && (btnLogin.Sensitive)){
				btnLogin.Click();				
			}
		}
		
		protected virtual void OnKeyReleased (object o, Gtk.KeyReleaseEventArgs args)
		{
			switch( args.Event.Key){
			case Gdk.Key.Escape:
				 btnCancelar.Click();
				break;
			}
		}
		
		
	}
}